class: center, middle, inverse, title-slide # Introduction to Git & GitHub ## Terminal, RStudio ### Andreas Scharmüller ### AG Landscape Ecology ### 2021-10-25 (updated: 2021-10-24) --- background-image: url(data:image/png;base64,#https://miro.medium.com/max/1240/0*6FETDgCn09uY2C4t.jpg) background-position: 80%, 30% background-size: 30% ## Why Git? - Version control software - Modern software development - most R-packages - Git is the __software__ - GitHub is the __platform__ #### Other Plattforms - GitLab: <https://about.gitlab.com> - BitBucket: <https://bitbucket.org> --- ## Install Git <https://github.com/git-guides/install-git> #### On Linux Ubuntu ```sh sudo apt update sudo apt install git-all ``` #### On windows Download manually ## Setup Local Git __Repository__ ```sh cd my_root_directory git init ``` --- ## Create a GitHub account <img src="data:image/png;base64,#figures/github0_account.png" width="700px" /> --- ## Create (initialize) repository #### Locally ```sh git init ``` #### Remote <img src="data:image/png;base64,#figures/github0_repo.png" width="600px" /> --- ## Create (initialize) repository <img src="data:image/png;base64,#figures/github0_repo2.png" width="600px" /> --- ## Fork a repository - Fork someone's repo to your GitHub account  --- ## Clone a repository - Clone __your__ fork (i.e. version) to your local machine  --- ## Clone a repository #### Command line ```sh cd Projects git clone git@github.com:andschar/MOD3.git # SSH git clone https://github.com/andschar/MOD3.git # HTTPS ``` #### RStudio <img src="data:image/png;base64,#figures/github3_clone2.png" width="400px" /> --- ## RStudio  --- ## RStudio  --- class: center, inverse, middle ## First repository (i.e. fork) !! <img src="data:image/png;base64,#figures/git_repo_hooray.jpg" width="400px" /> --- class: inverse, center, middle ## Local changes Make changes to the `.txt`, `.R`, `.python` or to whatever file... <img src="data:image/png;base64,#figures/github4_local_cahnges.gif" width="400px" /> --- ## Stage your changes ```sh git add file.txt git add *.txt git add -A ```  --- ## Commit changes ```sh git commit -m 'Patch-1' ```  --- ## Push to your repo (origin) ```sh git push origin master git push origin main ```  --- ## Pull Request #### From your main branch to the remote main branch  --- ## Git/Github Workflow <img src="data:image/png;base64,#figures/GIT_workflow.png" width="800px" /> --- ## Git/Github Workflow 1. Create or Fork a repo 2. Clone the repo to local machine 3. Local changes 4. Stage (i.e. add) & Commit changes to Git tree 5. Push to origin 6. Create a Pull-Request against the remote repository --- class: center, inverse, middle # Branches <img src="data:image/png;base64,#figures/git_branch.jpg" width="600px" /> --- ## Branch #### Create/Switch Branch ```sh git checkout -b dev_new_feature git checkout master git status git branch ```  --- ## Branch <img src="data:image/png;base64,#figures/GIT_workflow_branches.png" width="700px" /> --- ## Branch #### Merge Branch ```sh git checkout master git merge dev_new_feature ``` --- ## Branch #### Merge example First 8 commits of my R-package repo: __andschar/dbreport__ ```sh git log --all --decorate --oneline --graph ``` <img src="data:image/png;base64,#figures/github9_adog.png" width="600px" /> --- ## Branch #### Pull / Merge Request From your dev_XXX branch to a new remote dev_XXX branch  --- class: inverse, center, middle # R solutions --- ## {usethis} & {gert} package ```r gert::git_commit() usethis::pr_pull() usethis::pr_push() ``` <img src="data:image/png;base64,#https://www.garrickadenbuie.com/blog/pull-request-flow-usethis/usethis-pr-flow-medium.jpg" width="650px" /> .foot-note[ <https://www.garrickadenbuie.com/blog/pull-request-flow-usethis> ] --- background-image: url(data:image/png;base64,#https://i.redd.it/wr308dmuydd71.png) background-size: 40% background-position: 90%, 50% ## {usethis} & {gert} package ```r gert::git_commit() usethis::pr_pull() usethis::pr_push() ``` - Convenience __wrapper__ around git - Advantage: - Easier to use - Disadvantage: - More software to maintain - Might break something in the future - You might not always use R in your career --- class: center, inverse, middle <img src="data:image/png;base64,#figures/boromir-git.jpeg" width="650px" /> --- ## Material - OpenOlat - StackOverflow - YouTube - <https://guides.github.com/activities/hello-world> - <https://support.rstudio.com/hc/en-us/articles/200532077-Version-Control-with-Git-and-SVN> - <https://ohshitgit.com> - <https://www.garrickadenbuie.com/blog/pull-request-flow-usethis> ### Made with - <https://github.com/rstudio/rmarkdown> - <https://github.com/yihui/knitr> - <https://github.com/yihui/xaringan> --- ## Task: RMarkdown & Git - __Fork__ the MOD3 test repo: <https://github.com/andschar/MOD3> - __Clone__ the repo to your local machine - Create a new branch named dev_YOURSURNAME - Change the file _task.Rmd_ according to the steps described in the file - Create (i.e. knit) a .html out of the .Rmd file - __Stage__ & __Commit__ both files in the new branch - __Push__ your local changes to your repo (HINT: this might cause some trouble ;)) - Create a __Pull Request__ against my original repo <andschar/MOD3-student> --- layout: false # [Introduction to Git & GitHub](https://andschar.github.io/teaching/GIT) ## Thank you for your attention! ### Slides: <https://andschar.github.io/teaching/GIT> __Andreas Scharmüller__<br> Quantitative Landscape Ecology<br/> iES Landau, Institute for Environmental Sciences<br> University of Koblenz-Landau<br> University of Strasbourg <img src="data:image/png;base64,#https://upload.wikimedia.org/wikipedia/de/thumb/9/9f/Twitter_bird_logo_2012.svg/1259px-Twitter_bird_logo_2012.svg.png" width="20"> @andschar <br/> <img src="https://seeklogo.com/images/M/mail-icon-logo-28FE0635D0-seeklogo.com.png" width="20"> andschar@protonmail.com <img src="https://www.uni-koblenz-landau.de/de/landau/fb7/umweltwissenschaften/logos/land-ecol-neu.jpg" width="100"> <img src="https://www.uni-koblenz-landau.de/de/uni/organisation/verwaltung/abteilungen/abt-1/dokumente-ab1/uni-logo-farbig.jpg" width="200">